# -*- coding:utf-8 -*-
from pygame.locals import *
import pygame
import random
import easygui
from fractions import Fraction
from consts import *
from myclasses import *
import time as t
from collections import defaultdict
from dynamic_objects import *

hit_enemy2_cnt_dict = defaultdict(int)

while True:
    if run:
        # 控制最大帧数
        clock.tick(max_rate)

        # 导入文字
        text = '分数:' + str(number)
        text2 = '时间:' + str(time)
        text3 = '难度等级:' + str(level)
        text4 = '历史最高分数:' + str(date)
        tu = '按空格键可暂停'
        textImage = myfont.render(text, True, black)
        textimage2 = myfont.render(text2, True, black)
        text3image = myfont.render(text3, True, black)
        text4image = myfont.render(text4, True, black)
        tuimage = myfont.render(tu, True, black)

        if pygame.mixer.music.get_busy() == False:
            pygame.mixer.music.play()

        back.display()

        if number < 200:
            level = 1
        elif number >= 200 and number < 500:
            level = 2
        elif number >= 500 and number < 1000:
            level = 3
        elif number >= 1000 and number < 2000:
            level = 4
        elif number >= 2000 and number < 5000:
            level = 5
        elif number >= 5000:
            level = 'max'

        # 难度
        if level == 1:
            speed_1 = 30
            speed_2 = 60
        elif level == 2:
            speed_1 = 20
            speed_2 = 30
        elif level == 3:
            speed_1 = 15
            speed_2 = 20
        elif level == 4:
            speed_1 = 10
            speed_2 = 15
        elif level == 5:
            speed_1 = 5
            speed_2 = 10
        elif level == 'max':
            speed_2 = 5

        screen.blit(textImage, (0, 0))
        screen.blit(textimage2, (0, 20))
        screen.blit(text3image, (0, 40))
        screen.blit(text4image, (0, 60))
        screen.blit(tuimage, (335, 0))

        if ticks >= time_rate:
            ticks = 0
        if player.is_hit:
            if ticks % (time_rate // 2) == 0:
                player_down_index += 1
            player.image = plane[player_down_index]
            if player_down_index == 5:
                break
        else:
            player.image = plane[ticks // (time_rate // 2)]
        if player.image == player_plane1:
            player.move(True)
        elif player.image == player_plane2:
            player.move(False)
        screen.blit(player.image, player.rect)
        if ticks % pa_speed == 0:
            player.shoot(pa)
            if gift_bool == 'running':
                pa_speed = 2
                gift_num -= 1
                if gift_num <= 0:
                    gift_bool = 'stop'
                    pa_speed = 10

        player.pas.update()
        if ticks % speed_1 == 0:
            enemy1 = Enemy(enemy, [random.randint(0, l - enemy.get_width()), -enemy.get_height()])
            enemy1_group.add(enemy1)
        if ticks % speed_2 == 0:
            enemy222 = Enemy2(enemy22, [random.randint(0, l - enemy22.get_width()), -enemy22.get_height()], 6)
            enemy2_group.add(enemy222)
        if time % 20 == 0:
            gift1 = Gift(gift, [random.randint(0, l - gift.get_width()), -gift.get_height()])
            gift_group.add(gift1)
        if gift_bool == 'running':
            if ticks % 6 == 0:
                maxpa = Max_pa(max_pa, [random.randint(0, l - max_pa.get_width()), w - max_pa.get_height()])
                max_pa_group.add(maxpa)
            max_pa_group.draw(screen)
            max_pa_group.update()

        # 音乐卡点重播
        '''
        if time % 55==0:
            pygame.mixer.music.stop()
            pygame.mixer.music.play()
        '''

        enemy1_group.update()
        enemy1_group.draw(screen)
        enemy2_group.update()
        enemy2_group.draw(screen)
        gift_group.update()
        gift_group.draw(screen)

        player.pas.draw(screen)
        ticks += 1

        n += float(Fraction(1, 60))
        time = round(n, 2)

        # 敌机与子弹的碰撞
        enemy1_group_down.add(pygame.sprite.groupcollide(enemy1_group, player.pas, True, True))
        for enemy1_down in enemy1_group_down:
            screen.blit(enemy_down_1[enemy1_down.down_index], enemy1_down.rect)
            if ticks % (time_rate // 2) == 0:
                if enemy1_down.down_index < 3:
                    enemy1_down.down_index += 1
                else:
                    enemy1_group_down.remove(enemy1_down)
                    number += 2
        # if ticks % (time_rate // 2) == 0:
        hit_e2_list = pygame.sprite.groupcollide(enemy2_group, player.pas, False, True)
        for e2 in hit_e2_list:
            e2.hit_count += 1
            print(e2.hit_count,e2.max_hit_count,len(enemy2_group_down))
            if e2.hit_count > e2.max_hit_count:
                enemy2_group.remove(e2)
                enemy2_group_down.add(e2)
                # else:
                #     enemy2_group_down.remove(e2)
        # enemy2_group_down.add(pygame.sprite.groupcollide(enemy2_group, player.pas, True, True))
        for enemy00 in enemy2_group_down:
            screen.blit(enemy22_down[enemy00.down2_index], enemy00.rect)
            # enemy00.hit_count += 1
            # print(enemy00.hit_count)
            hit_enemy2_cnt_dict[enemy00] += 1

            if ticks % (time_rate // 2) == 0:
                if enemy00.down2_index < 3:
                    enemy00.down2_index += 1
                else:
                    enemy2_group_down.remove(enemy00)
                    number += 5

        # 敌机与超级子弹的碰撞
        if gift_bool == 'running':
            enemy1_group_down.add(pygame.sprite.groupcollide(enemy1_group, max_pa_group, True, True))
            for enemy1_down in enemy1_group_down:
                screen.blit(enemy_down_1[enemy1_down.down_index], enemy1_down.rect)

                if ticks % (time_rate // 2) == 0:
                    if enemy1_down.down_index < 3:
                        enemy1_down.down_index += 1
                    else:
                        enemy1_group_down.remove(enemy1_down)
                        number += 2
            # if ticks % (time_rate // 2) == 0:
            hit_e2_list = pygame.sprite.groupcollide(enemy2_group, max_pa_group, False, True)
            for e2 in hit_e2_list:
                e2.hit_count += 1
                print(e2.hit_count,e2.max_hit_count,len(enemy2_group_down))
                if e2.hit_count > e2.max_hit_count:
                    enemy2_group.remove(e2)
                    enemy2_group_down.add(e2)
                    # else:
                    #     enemy2_group_down.remove(e2)
            for enemy00 in enemy2_group_down:
                screen.blit(enemy22_down[enemy00.down2_index], enemy00.rect)
                if ticks % (time_rate // 2) == 0:
                    if enemy00.down2_index < 3:
                        enemy00.down2_index += 1
                    else:
                        enemy2_group_down.remove(enemy00)
                        number += 5

        # 补给的控制
        if not haha:
            haha = pygame.sprite.groupcollide(player_group, gift_group, False, True)
        gift_down_group.add(pygame.sprite.groupcollide(player_group, gift_group, False, True))
        if haha:
            gift_down_group.remove(pygame.sprite.groupcollide(player_group, gift_group, False, True))
            gift_bool = 'start'
            haha = False
        if gift_bool == 'start':
            gift_num = 240
            gift_bool = 'running'

        # 玩家与敌机的碰撞

        if time > 5:
            enemy1_down_list = pygame.sprite.spritecollide(player, enemy1_group, True)
            if len(enemy1_down_list) > 0:
                enemy1_group_down.add(enemy1_down_list)
                player.is_hit = True
            enemy2_down_list = pygame.sprite.spritecollide(player, enemy2_group, True)
            if len(enemy2_down_list) > 0:
                enemy2_group_down.add(enemy2_down_list)
                player.is_hit = True

        pygame.display.update()

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    run = False
                    if easygui.ccbox('游戏暂停', choices=['回到游戏', '退出']):
                        run = True
                    else:
                        exit()
if number > int(date):
    with open(r'历史最高纪录.txt', "w") as f:
        f.write(str(number))
    f.close()
    kkk = number
else:
    kkk = int(date)
if easygui.ccbox(
        '分数:' + str(number) + '\n' + '时间:' + str(time) + '\n' + '难度等级:' + str(level) + '\n' + '历史最高分:' + str(kkk),
        choices=['退出', '等会']):
    exit()
else:
    easygui.msgbox('还是算了吧,你呆在这个界面也没用')
    exit()
